﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;

namespace SerialPortWindow
{
    public partial class Form1 : Form
    {
        private static Form1 _instance;
        public static Form1 Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new Form1();
                }
                return _instance;
            }
        }

        private bool logScroll = true;
        private bool receiveScroll = true;

        private Queue<string> logQueue;

        private Form1()
        {
            logQueue = new Queue<string>();
            InitializeComponent();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            SerialPortModule.Instance.Close();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            SerialPortModule.Instance.Init();
            Timer timer1 = new Timer();
            timer1.Interval = 1;
            timer1.Enabled = true;
            timer1.Tick += new EventHandler(timer1_Tick_1);//添加事件
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.Send(textSend.Text);
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.Connect(textCom.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextLog.Clear();
        }

        public static void Log(string msg)
        {
            _instance.logQueue.Enqueue(msg);
        }

        private static void ShowLog(string msg)
        {
            _instance.richTextLog.AppendText(msg + Environment.NewLine);
            if (_instance.logScroll)
            {
                _instance.richTextLog.ScrollToCaret();
            }
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.Disconnect();
        }

        private void btnLogScroll_Click(object sender, EventArgs e)
        {
            logScroll = !logScroll;
        }

        private void btnReceiveScroll_Click(object sender, EventArgs e)
        {
            receiveScroll = !receiveScroll;
        }

        private void dropDownProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dropDownProject.SelectedIndex == 0)
            {
                SerialPortModule.Instance.project = 7;
                groupBox1.Show();
            }
            else if (dropDownProject.SelectedIndex == 1)
            {
                SerialPortModule.Instance.project = 5;
                groupBox1.Hide();
            }
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            if (logQueue.Count > 0)
            {
                string log = logQueue.Dequeue();
                ShowLog(log);
            }
        }

        private void btnCalibrate_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.Calibrate();
        }

        private void btnCheckVersion_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.Send("VER");
        }

        private void btnJSRight_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.SendCalibrate(textSend.Text, Sensor.right);
        }

        private void btnJSLeft_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.SendCalibrate(textSend.Text, Sensor.left);
        }

        private void btnLeftUp_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.SendCalibrate(textSend.Text, Sensor.leftUp);
        }

        private void btnLeftBottom_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.SendCalibrate(textSend.Text, Sensor.leftBottom);
        }

        private void btnRightUp_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.SendCalibrate(textSend.Text, Sensor.rightUp);
        }

        private void btnRightBottom_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.SendCalibrate(textSend.Text, Sensor.rightBottom);
        }

        private void btnJSRightClear_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.SendClear(Sensor.right);
        }

        private void btnJSLeftClear_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.SendClear(Sensor.left);
        }

        private void btnLeftUpClear_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.SendClear(Sensor.leftUp);
        }

        private void btnLeftBottomClear_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.SendClear(Sensor.leftBottom);
        }

        private void btnRightUpClear_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.SendClear(Sensor.rightUp);
        }

        private void btnRightBottomClear_Click(object sender, EventArgs e)
        {
            SerialPortModule.Instance.SendClear(Sensor.rightBottom);
        }
    }
}
